﻿using System.Collections.Generic;
using Newtonsoft.Json;
using OrkutAPILibrary.Constants;
using OrkutAPILibrary.Entities;

namespace OrkutAPILibrary.Methods
{
    /// <summary>
    /// Orkut Scrap Factory
    /// </summary>
    static class OrkutScrapFactory
    {
        /// <summary>
        /// Gets the self scraps.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutScrap> GetSelfScraps(int count = OrkutLibraryConstants.DefaultNoOfScraps)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "messages.get"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"pageType", "first"},
                                    {"messageType", "public_message"},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutScrap>>(requestResponse);
        }

        /// <summary>
        /// Gets the next set of self scraps.
        /// </summary>
        /// <param name="lastScrapId">The last scrap id.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutScrap> GetNextSetOfSelfScraps(string lastScrapId, int count = OrkutLibraryConstants.DefaultNoOfScraps)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "messages.get"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"pageType", "next"},
                                    {"lastKey", lastScrapId},
                                    {"messageType", "public_message"},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutScrap>>(requestResponse);
        }

        /// <summary>
        /// Gets the previous set of self scraps.
        /// </summary>
        /// <param name="firstScrapId">The first scrap id.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutScrap> GetPreviousSetOfSelfScraps(string firstScrapId, int count = OrkutLibraryConstants.DefaultNoOfScraps)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "messages.get"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"pageType", "prev"},
                                    {"lastKey", firstScrapId},
                                    {"messageType", "public_message"},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutScrap>>(requestResponse);
        }

        /// <summary>
        /// Gets the scraps of user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutScrap> GetScrapsOfUser(string userId, int count = OrkutLibraryConstants.DefaultNoOfScraps)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "messages.get"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"pageType", "first"},
                                    {"messageType", "public_message"},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutScrap>>(requestResponse);
        }

        /// <summary>
        /// Gets the next set of scraps of user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="lastScrapId">The last scrap id.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutScrap> GetNextSetOfScrapsOfUser(string userId, string lastScrapId, int count = OrkutLibraryConstants.DefaultNoOfScraps)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "messages.get"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"pageType", "next"},
                                    {"lastKey", lastScrapId},
                                    {"messageType", "public_message"},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutScrap>>(requestResponse);
        }

        /// <summary>
        /// Gets the previous set of scraps of user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="firstScrapId">The first scrap id.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutScrap> GetPreviousSetOfScrapsOfUser(string userId, string firstScrapId, int count = OrkutLibraryConstants.DefaultNoOfScraps)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "messages.get"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"pageType", "prev"},
                                    {"lastKey", firstScrapId},
                                    {"messageType", "public_message"},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutScrap>>(requestResponse);
        }

        /// <summary>
        /// Deletes the scrap sent to oneself.
        /// </summary>
        /// <param name="scrapToDelete">The scrap to delete.</param>
        /// <returns></returns>
        internal static bool DeleteSelfScrap(OrkutScrap scrapToDelete)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "messages.delete"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"messageType", "public_message"},
                                    {"msgId", scrapToDelete.ScrapId},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }

        /// <summary>
        /// Writes the scrap to user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="scrapText">The scrap text.</param>
        /// <returns></returns>
        internal static bool WriteScrapToUser(string userId, string scrapText)
        {
            var scrapToWrite = new OrkutScrap { ScrapBody = scrapText };

            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "messages.create"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"messageType", "public_message"},
                                    {"message", JsonConvert.SerializeObject(scrapToWrite, Formatting.None, 
                                        new JsonSerializerSettings{NullValueHandling=NullValueHandling.Ignore})},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }

        /// <summary>
        /// Replies to scrap.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="scrapId">The scrap id.</param>
        /// <param name="scrapText">The scrap text.</param>
        /// <returns></returns>
        internal static bool ReplyToScrap(string userId, string scrapId, string scrapText)
        {
            var scrapToWrite = new OrkutScrap { ScrapBody = scrapText };

            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "messages.create"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"messageType", "public_message"},
                                    {"msgId", scrapId},
                                    {"message", JsonConvert.SerializeObject(scrapToWrite, Formatting.None, 
                                        new JsonSerializerSettings{NullValueHandling=NullValueHandling.Ignore})},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }
    }
}
